//
//  ThreeImagesTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "ThreeImagesTableViewCell.h"
#import "NewsModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kImageViewHeight 80
#define kImageViewWidth ([UIScreen mainScreen].bounds.size.width - 30) * 0.33
#define kImageViewY CGRectGetMaxY(_titleLabel.frame)

@interface ThreeImagesTableViewCell ()

@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *commentLabel;
@property (nonatomic,strong) UIImageView *thirdImageView;
@property (nonatomic,strong) UIImageView *firstImageView;
@property (nonatomic,strong) UIImageView *secondImageView;

@end


@implementation ThreeImagesTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backView = [[UIView alloc] init];
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 10;
        _backView.layer.borderColor = [UIColor grayColor].CGColor;
        _backView.layer.borderWidth = 1;
        [self.contentView addSubview:_backView];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self.backView addSubview:_titleLabel];
        
        _commentLabel = [[UILabel alloc] init];
        _commentLabel.textAlignment = NSTextAlignmentRight;
        _commentLabel.font = [UIFont systemFontOfSize:14];
        _commentLabel.textColor = [UIColor grayColor];
        [self.backView addSubview:_commentLabel];
        
        _firstImageView = [[UIImageView alloc] init];
        [_backView addSubview:_firstImageView];

        _secondImageView = [[UIImageView alloc] init];
        [_backView addSubview:_secondImageView];
        
        _thirdImageView = [[UIImageView alloc] init];
        [_backView addSubview:_thirdImageView];
    }
    return self;
}

- (void)setModel:(NewsModel *)model
{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(5);
        make.right.and.bottom.mas_equalTo(-5);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView.mas_left).offset(5);
        make.top.equalTo(self.backView.mas_top).offset(5);
        make.right.mas_equalTo(-80);
        make.bottom.equalTo(self.backView.mas_top).offset(35);
    }];
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(5);
        make.top.equalTo(self.titleLabel);
        make.right.mas_equalTo(-5);
        make.bottom.equalTo(self.titleLabel);
    }];
    
    [self.firstImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView.mas_left).offset(5);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.right.mas_equalTo(-(kScreenWidth * 0.655));
        make.bottom.mas_equalTo(-5);
    }];
    
    [self.secondImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.firstImageView.mas_right).offset(5);
        make.size.and.top.equalTo(self.firstImageView);
    }];
    
    [self.thirdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.secondImageView.mas_right).offset(5);
        make.bottom.and.top.equalTo(self.secondImageView);
        make.right.mas_equalTo(-5);
    }];
    
    if (_model != model) {
        self.titleLabel.text = model.title;
        // 如果回复太多就改成几点几万
        CGFloat count =  [model.replyCount intValue];
        NSString *displayCount = nil;
        if (count > 10000) {
            displayCount = [NSString stringWithFormat:@"%.1f万跟帖",count/10000];
        }else{
            displayCount = [NSString stringWithFormat:@"%.0f跟帖",count];
        }
        self.commentLabel.text = displayCount;
        
        [self.firstImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"picholder"]];
        if (model.imgextra.count == 2) {
            [self.thirdImageView setImageWithURL:[NSURL URLWithString:model.imgextra[1][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"picholder"]];
            [self.secondImageView setImageWithURL:[NSURL URLWithString:model.imgextra[0][@"imgsrc"]] placeholderImage:[UIImage imageNamed:@"picholder"]];
        }
    }
}

+ (instancetype)addThreeImagesCellWithTableView:(UITableView *)tableView model:(NewsModel *)model
{
    NSString *cellIdentifier = @"ImagesCell";
    ThreeImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ThreeImagesTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    cell.model = model;
    return cell;
}


@end
